package conan.presentation;

import conan.domain.Controller;
import conan.domain.KompetenceInterface;

import conan.util.Log;

import java.awt.*;
import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

public class OpretMedarbejderFrame
    extends JFrame
{

    private Log log = new Log(this);
    private Controller control;
    private JButton gemMedarbejderButton = new JButton();
    private JButton lukButton = new JButton();
    private JTextField jTextField2 = new JTextField();
    private JLabel jLabel1 = new JLabel();
    private DefaultListModel alleKompetenceModel = new DefaultListModel();
    private JList alleKompetencerList = new JList(alleKompetenceModel);
    private JLabel jLabel2 = new JLabel();
    private DefaultListModel kendteKompetenceModel = new DefaultListModel();
    private JList kendteKompetencerList = new JList(kendteKompetenceModel);
    private JButton addKompetenceButton = new JButton();
    private JLabel jLabel3 = new JLabel();
    private JScrollPane jScrollPane1 = new JScrollPane();
    private JScrollPane jScrollPane2 = new JScrollPane();
    private JButton removeKompetenceButton = new JButton();

    public OpretMedarbejderFrame()
    {
        try
        {
            jbInit();
            JFrame frame = this;
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = frame.getSize();
            if (frameSize.height > screenSize.height)
            {
                frameSize.height = screenSize.height;
            }
            if (frameSize.width > screenSize.width)
            {
                frameSize.width = screenSize.width;
            }
            frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        luk();
                    }


                });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void jbInit()
        throws Exception
    {
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(400, 300));
        this.setResizable(false);
        this.setTitle("Opret medarbejder");
        gemMedarbejderButton.setText("Gem Medarbejder");
        gemMedarbejderButton.setBounds(new Rectangle(20, 230, 120, 25));
        gemMedarbejderButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    gemMedarbejderButton_actionPerformed(e);
                }
            });
        lukButton.setText("Luk");
        lukButton.setBounds(new Rectangle(310, 230, 60, 25));
        lukButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    lukButton_actionPerformed(e);
                }
            });
        jTextField2.setBounds(new Rectangle(85, 25, 210, 25));
        jLabel1.setText("Navn");
        jLabel1.setBounds(new Rectangle(85, 5, 210, 20));
        jLabel1.setFont(new Font("Tahoma", 1, 11));
        jLabel2.setText("Alle Kompetencer");
        jLabel2.setBounds(new Rectangle(55, 70, 100, 20));
        jLabel2.setFont(new Font("Tahoma", 1, 11));
        addKompetenceButton.setText(">");
        addKompetenceButton.setBounds(new Rectangle(170, 120, 50, 25));
        addKompetenceButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    addKompetenceButton_actionPerformed(e);
                }
            });
        jLabel3.setText("Kendte Kompetencer");
        jLabel3.setBounds(new Rectangle(225, 70, 120, 20));
        jLabel3.setFont(new Font("Tahoma", 1, 11));
        jScrollPane1.setBounds(new Rectangle(50, 90, 110, 115));
        jScrollPane2.setBounds(new Rectangle(230, 90, 110, 115));
        removeKompetenceButton.setText("<");
        removeKompetenceButton.setBounds(new Rectangle(170, 150, 50, 25));
        removeKompetenceButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    removeKompetenceButton_actionPerformed(e);
                }
            });
        jScrollPane1.getViewport().add(alleKompetencerList, null);
        this.getContentPane().add(removeKompetenceButton, null);
        this.getContentPane().add(jScrollPane1, null);
        this.getContentPane().add(jLabel3, null);
        this.getContentPane().add(addKompetenceButton, null);
        this.getContentPane().add(jLabel2, null);
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(jTextField2, null);
        this.getContentPane().add(lukButton, null);
        this.getContentPane().add(gemMedarbejderButton, null);
        jScrollPane2.getViewport().add(kendteKompetencerList, null);
        this.getContentPane().add(jScrollPane2, null);
    }

   
    private void gemMedarbejderButton_actionPerformed(ActionEvent e)
    {
        String nyMedarbejderNavn = jTextField2.getText();
        ArrayList<KompetenceInterface> kompetenceListe = new ArrayList<KompetenceInterface>();
        for (int i = 0; i < kendteKompetenceModel.size(); i++)
        {
            kompetenceListe.add((KompetenceInterface) kendteKompetenceModel.getElementAt(i));
        }
        if (control.opretNyMedarbejder(nyMedarbejderNavn, kompetenceListe))
        {
            log.sendMsg("Medarbejder gemt i DB");
            this.setVisible(false);
            ((JFrame) this).dispose();
        }
        else
        {
            log.sendMsg("Fejl, medarbejder ikke gemt.");
        }
    }


    public void aabn(Controller control)
    {
        this.control = control;
        this.control.useCaseStart();
        this.setVisible(true);
        for (KompetenceInterface ki: control.getKompetenceList())
        {
            int position = alleKompetenceModel.size();
            alleKompetenceModel.add(position, ki);
        }

    }

    private void addKompetenceButton_actionPerformed(ActionEvent e)
    {
        int tal = alleKompetencerList.getSelectedIndex();
        KompetenceInterface ki = null;

        if (-1 != tal)
        {
            ki = (KompetenceInterface) alleKompetenceModel.getElementAt(tal);
        }

        if (null != ki)
        {
            kendteKompetenceModel.addElement(ki);
            alleKompetenceModel.removeElement(ki);
        }
    }

    private void lukButton_actionPerformed(ActionEvent e)
    {
        luk();
        this.dispose();
    }

    private void luk()
    {
        control.useCaseAfslutUdenCommit();
    }

    private void removeKompetenceButton_actionPerformed(ActionEvent e)
    {
        int tal = kendteKompetencerList.getSelectedIndex();
        KompetenceInterface ki = null;

        if (-1 != tal)
        {
            ki = (KompetenceInterface) kendteKompetenceModel.getElementAt(tal);
        }

        if (null != ki)
        {
            alleKompetenceModel.addElement(ki);
            kendteKompetenceModel.removeElement(ki);
        }
    }
}
